package com.yycx.starter.ocr.utils;

import com.yycx.common.base.utils.FlymeUtils;
import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.text.PDFTextStripper;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * pdf处理
 * @author zyf
 */
public class PdfUtils {

    private static String getContent(String path) throws Exception {
        //创建输入流对象
        FileInputStream fileInputStream = new FileInputStream(path);
        //创建解析器对象
        PDFParser pdfParser = new PDFParser(new RandomAccessBuffer(fileInputStream));
        pdfParser.parse();
        //pdf文档
        PDDocument pdDocument = pdfParser.getPDDocument();
        //pdf文本操作对象,使用该对象可以获取所读取pdf的一些信息
        PDFTextStripper pdfTextStripper = new PDFTextStripper();
        String content = pdfTextStripper.getText(pdDocument);
        //PDDocument对象时使用完后必须要关闭
        pdDocument.close();
        return content;
    }

    public static List<String> pdfPathToImagePaths(String pdfPath) throws IOException {
        File pdfFile = new File(pdfPath);
        PDDocument pdDocument = PDDocument.load(pdfFile);
        int pageCount = pdDocument.getNumberOfPages();
        PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
        List<String> imagePathList = new ArrayList<>();
        String fileParent = pdfFile.getParent();
        for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
            String imgPath = fileParent + File.separator + UUID.randomUUID().toString() + ".png";
            BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 80, ImageType.RGB);
            ImageIO.write(image, "png", new File(imgPath));
            imagePathList.add(imgPath);
            System.out.println("第张生成的图片路径为{}" + pageIndex + imgPath);
        }
        pdDocument.close();
        return imagePathList;
    }


    public static List<BufferedImage> pdfToBufferedImageList(String pdfPath) throws IOException {
        File pdfFile = new File(pdfPath);
        return pdfToBufferedImageList(pdfFile);
    }

    public static List<BufferedImage> pdfToBufferedImageList(File file) throws IOException {
        List<BufferedImage> bufferedImageList = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(file)) {
            PDDocument pdDocument = PDDocument.load(file);
            int pageCount = pdDocument.getNumberOfPages();
            PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
            for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
                BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 80, ImageType.RGB);
                bufferedImageList.add(image);
            }
            pdDocument.close();
        }
        return bufferedImageList;
    }


    public static void main(String[] agrs) {
        try {
            //String content=   getContent("D:\\test.pdf");
            pdfPathToImagePaths("D:\\test.pdf");
            //System.out.println(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
